package com.example.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.adapter.GridViewAdapter;
import com.example.application.BaseActivity;
import com.example.bean.GridViewBean;
import com.example.db.DBService;
import com.example.zhaccount.MainActivity;
import com.example.zhaccount.R;

public class BillDetail_Activity extends BaseActivity implements OnClickListener{
	/**
	 * 初始化的界面控件
	 */
	private GridViewBean mGVBbean;
	private List<GridViewBean> mList;
	private GridViewBean mdateGVB;
	private GridViewAdapter dtl_adapter;
	private int mState;
	private Integer[] imgs;
	private String[] names;
	
	private ImageView dtl_imgback,dtl_changeImg;
	private TextView dtl_tvdate,dtl_changename,dtl_changeText;
	private EditText dtl_mark;
	private GridView dtl_gridView;
	private OnItemClickListener myItemListener;			//单击监听函数
	private int mIndex;
	
	private static final int SHOW_DATE = 15;
	/**
	 * 计算器变量
	 */
	private TextView one,four,seven,AC,two,five,eight,zero,three,six,nine,point,add,equ,ok;
	private boolean isChoosed = true;
	private String intext1 = null;
	private String intext2 = null;
	private String intext3 = null;
	private String intext4 = null;
	private String intext5 = null;
	private String intext6 = null;
	private String intext7 = null;
	private String intext8 = null;
	private String intext9 = null;
	private String intext0 = null;
	private String intextpoint = null;
	
	private double result =0;
	private double num1 = 0;
	private double num2 = 0;
	private int op = 0;
	
	private DBService mdbService;
	private long mE;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bill_detail);
		
		mGVBbean = (GridViewBean) getIntent().getSerializableExtra("chooseGVB");
		mState = Integer.parseInt(mGVBbean.getState());
		
		findView();
		if (mState == 1) {
			init();
		}else{
			initOther();
		}
		initDate();
		initCalcult();
	}
	
	public void findView(){
		dtl_imgback = (ImageView) findViewById(R.id.imgback);
		dtl_tvdate = (TextView) findViewById(R.id.date);
		dtl_mark = (EditText) findViewById(R.id.mark);
		dtl_gridView = (GridView) findViewById(R.id.gridview);
		dtl_changeImg = (ImageView) findViewById(R.id.change_img);
		dtl_changename = (TextView) findViewById(R.id.change_name);
		dtl_changeText =(TextView) findViewById(R.id.change_bill);
		
		dtl_imgback.setOnClickListener(this);
		dtl_tvdate.setOnClickListener(this);
		myItemListener = new myItemListener();
		dtl_gridView.setOnItemClickListener(myItemListener);
	}
	
	public void init(){
		imgs = new Integer[]{R.drawable.type_big_1, R.drawable.type_big_2, R.drawable.type_big_3,
				R.drawable.type_big_4, R.drawable.type_big_5, R.drawable.type_big_6, R.drawable.type_big_7,
				R.drawable.type_big_8, R.drawable.type_big_9, R.drawable.type_big_10, R.drawable.type_big_11,
				R.drawable.type_big_12};
		names = new String[]{"一般", "用餐", "零食", "交通", "充值", 
				"购物", "娱乐", "住房", "约会", "网购", "日用品", "香烟"};
		mList = new ArrayList<GridViewBean>();
		mdateGVB = new GridViewBean(imgs, names);
		for (int i = 0; i < imgs.length; i++) {
			mList.add(mdateGVB);
		}
		dtl_adapter = new GridViewAdapter(mList, this);
		dtl_gridView.setAdapter(dtl_adapter);
	}
	
	public void initOther(){
		imgs = new Integer[]{R.drawable.type_big_13, R.drawable.type_big_14, R.drawable.type_big_15,
				R.drawable.type_big_16, R.drawable.type_big_17, R.drawable.type_big_18, R.drawable.type_big_19};
		names = new String[]{"工资", "外快兼职", "奖金", "借入", "零花钱", "投资收入", "礼物红包"};
		mList = new ArrayList<GridViewBean>();
		mdateGVB = new GridViewBean(imgs, names);
		for (int i = 0; i < imgs.length; i++) {
			mList.add(mdateGVB);
		}
		dtl_adapter = new GridViewAdapter(mList, this);
		dtl_gridView.setAdapter(dtl_adapter);
	}
	
	//第一次进来时的界面
	public void initDate(){
		dtl_tvdate.setText(mGVBbean.getTime());
		dtl_mark.setText(mGVBbean.getMark());
		dtl_changeImg.setImageResource(mGVBbean.getImg());
		dtl_changename.setText(mGVBbean.getType());
		dtl_changeText.setText(mGVBbean.getMoney());
		
		mdbService = new DBService(this);
	}
	
	public void initCalcult(){
		one = (TextView) findViewById(R.id.one);
		one.setOnClickListener(this);
		two = (TextView) findViewById(R.id.two);
		two.setOnClickListener(this);
		three = (TextView) findViewById(R.id.three);
		three.setOnClickListener(this);
		four = (TextView) findViewById(R.id.four);
		four.setOnClickListener(this);
		five = (TextView) findViewById(R.id.five);
		five.setOnClickListener(this);
		six = (TextView) findViewById(R.id.six);
		six.setOnClickListener(this);
		seven = (TextView) findViewById(R.id.seven);
		seven.setOnClickListener(this);
		eight = (TextView) findViewById(R.id.eight);
		eight.setOnClickListener(this);
		nine = (TextView) findViewById(R.id.nine);
		nine.setOnClickListener(this);
		zero = (TextView) findViewById(R.id.zero);
		zero.setOnClickListener(this);
		AC = (TextView) findViewById(R.id.clear);
		AC.setOnClickListener(this);
		add = (TextView) findViewById(R.id.add);
		add.setOnClickListener(this);
		equ = (TextView) findViewById(R.id.equ);
		equ.setOnClickListener(this);
		point = (TextView) findViewById(R.id.dian);
		point.setOnClickListener(this);
		ok = (TextView) findViewById(R.id.ok);
		ok.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imgback:
			finish();
			break;
		case R.id.date:
			showDialog(SHOW_DATE);
			break;
		case R.id.one:
			if (isChoosed) {
				dtl_changeText.setText(null);
				isChoosed = false;
			}
			intext1 = dtl_changeText.getText().toString();
			intext1 += "1";
			dtl_changeText.setText(intext1);
			break;
		case R.id.two:
			if (isChoosed) {
				dtl_changeText.setText(null);
				isChoosed = false;
			}
			intext2 = dtl_changeText.getText().toString();
			intext2 += "2";
			dtl_changeText.setText(intext2);
			break;
		case R.id.three:
			if (isChoosed) {
				dtl_changeText.setText(null);
				isChoosed = false;
			}
			intext3 = dtl_changeText.getText().toString();
			intext3 += "3";
			dtl_changeText.setText(intext3);
			break;
		case R.id.four:
			if (isChoosed) {
				dtl_changeText.setText(null);
				isChoosed = false;
			}
			intext4 = dtl_changeText.getText().toString();
			intext4 += "4";
			dtl_changeText.setText(intext4);
			break;
		case R.id.five:
			if (isChoosed) {
				dtl_changeText.setText(null);
				isChoosed = false;
			}
			intext5 = dtl_changeText.getText().toString();
			intext5 += "5";
			dtl_changeText.setText(intext5);
			break;
		case R.id.six:
			if (isChoosed) {
				dtl_changeText.setText(null);
				isChoosed = false;
			}
			intext6 = dtl_changeText.getText().toString();
			intext6 += "6";
			dtl_changeText.setText(intext6);
			break;
		case R.id.seven:
			if (isChoosed) {
				dtl_changeText.setText(null);
				isChoosed = false;
			}
			intext7 = dtl_changeText.getText().toString();
			intext7 += "7";
			dtl_changeText.setText(intext7);
			break;
		case R.id.eight:
			if (isChoosed) {
				dtl_changeText.setText(null);
				isChoosed = false;
			}
			intext8 = dtl_changeText.getText().toString();
			intext8 += "8";
			dtl_changeText.setText(intext8);
			break;
		case R.id.nine:
			if (isChoosed) {
				dtl_changeText.setText(null);
				isChoosed = false;
			}
			intext9 = dtl_changeText.getText().toString();
			intext9 += "9";
			dtl_changeText.setText(intext9);
			break;
		case R.id.zero:
			if (isChoosed) {
				dtl_changeText.setText(null);
				isChoosed = false;
			}
			intext0 = dtl_changeText.getText().toString();
			intext0 += "0";
			dtl_changeText.setText(intext0);
			break;
		case R.id.dian:
			intextpoint = dtl_changeText.getText() + ".";
			dtl_changeText.setText(intextpoint);
			break;
		case R.id.add:
			if (dtl_changeText.getText().toString().equals(null)
					|| dtl_changeText.getText().toString().equals("")) {
				return;
			}
			String s = dtl_changeText.getText().toString();
			num1 = Double.valueOf(s);
			dtl_changeText.setText(null);
			isChoosed = false;
			op = 1;
			break;
		case R.id.equ:
			if (dtl_changeText.getText().toString().equals(null)
					|| dtl_changeText.getText().toString().equals("")) {
				return;
			}
			String ss = dtl_changeText.getText().toString();
			num2 = Double.valueOf(ss);
			if (op == 1) {
				result = num1 + num2;
			}
			dtl_changeText.setText(String.valueOf(result));
			isChoosed = true;
			op = 0;
			break;
		case R.id.clear:
			if (dtl_changeText.getText().toString().equals(null)
					|| dtl_changeText.getText().toString().equals("")) {
				return;
			}
			dtl_changeText.setText(null);
			isChoosed = false;
			break;
		case R.id.ok:
			if (dtl_changeText.getText().toString().equals(null)
					|| dtl_changeText.getText().toString().equals("")) {
				return;
			}
			isChoosed = true;
			mGVBbean.setTime(dtl_tvdate.getText().toString());
			mGVBbean.setMark(dtl_mark.getText().toString());
			mGVBbean.setImg(mIndex);
			mGVBbean.setType(dtl_changename.getText().toString());
			mGVBbean.setMoney(dtl_changeText.getText().toString());
			mE = mdbService.updateBill(String.valueOf(mGVBbean.getId()), mGVBbean);
			if (mE != -1) {
				Intent it = new Intent(this, MainActivity.class);
				startActivity(it);
			}else{
				Toast.makeText(this, "update fail!", Toast.LENGTH_SHORT).show();
			}
			break;
		default:
			break;
		}
	}
	
	 // 选择日期
		DatePickerDialog.OnDateSetListener onDateSetListener = new OnDateSetListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
//				String time = new SimpleDateFormat("yyyy-MM-dd")
//									.format(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
				dtl_tvdate.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
			}
		};

		@Override
		protected Dialog onCreateDialog(int id, Bundle args) {
			Calendar localCal = Calendar.getInstance();
			int i = localCal.get(Calendar.YEAR);
			int j = localCal.get(Calendar.MONTH);
			int k = localCal.get(Calendar.DAY_OF_MONTH);
			if (id == SHOW_DATE) {
				Log.i("zmy", "i--j--k----->"+i+"  "+j+"  "+k);
				return new DatePickerDialog(this, onDateSetListener, i, j, k);
			}
			return null;
		}
	
	//GridView的单击事件
	class myItemListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			mIndex = position;
			changeImgName(mdateGVB,mIndex);
		}

	}

	/**
	 * GridView单击事件，改变图片跟类型
	 * @param mdateGVB
	 * @param mIndex
	 */
	private void changeImgName(GridViewBean bean, int position) {
		int path = bean.getImgs()[position];
		dtl_changeImg.setImageResource(path);
		String txt = bean.getTypes()[position];
		dtl_changename.setText(txt);
	}
}
